home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / MIDI Manager Class Library / CMIDIPort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-03  |  6.2 KB  |  196 lines  |  [TEXT/KAHL]

  1. /*
  2.  *--- CMIDIPort.c ----------------------------------------------------------------------
  3.  * Copyright © Paul Ferguson, 1990, 1991, 1992.  All rights reserved.
  4.  *
  5.  * Superclass:  CObject
  6.  * Subclasses:  CDataPort CTimePort
  7.  *
  8.  * Description:
  9.  *    CMIDIPort.c defines a MIDI Manager port object.  CMIDIPort is an abstract type,
  10.  *    containing variables and methods common to all three port types.
  11.  *
  12.  *    For use with THINK C 5.0, the accompanying THINK Class Library (TCL), and MIDI
  13.  *    Manager 2.0. Refer to the accompanying Microsoft Word document for complete
  14.  *    details about MIDI Manager objects.
  15.  *
  16.  *    If you have comments or questions about this code, you can reach me on
  17.  *    CompuServe at 70441,3055.
  18.  *
  19.  *--------------------------------------------------------------------------------------
  20.  *---- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE ----
  21.  *--------------------------------------------------------------------------------------
  22.  *    If you are not familiar with programming the Apple MIDI Manager, refer to the
  23.  *    "MIDI Management Tools" Version 2.0, available from APDA.  You MUST have the
  24.  *    software (MIDI.H and the library) from this package in order to use these objects.
  25.  *    It will not work without this.
  26.  *--------------------------------------------------------------------------------------
  27.  *    REVISION HISTORY:
  28.  *        August ??, 1990            - Original release (1.0).
  29.  *        November 5, 1990        - Added checks for midiMgrVer to most methods.
  30.  *        August 1991                - updated for THINK C 5.0 as version 2.0
  31.  *--------------------------------------------------------------------------------------
  32.  */
  33.  
  34. #include "CMIDIPort.h"                    // This code's header file
  35.  
  36. extern    OSType    gSignature;                // Used to register client
  37. extern CMIDIClient * gMIDIClient;
  38.  
  39. /*
  40.  *--- CMIDIPort::IMIDIPort -------------------------------------------------------------
  41.  * This method is called by IMIDIInputPort(), IMIDIOutputPort(), and IMIDITimePort().
  42.  * When this method is called, the portParams structure should be completely filled in.
  43.  * 
  44.  * Note that this method checks for gMIDIClient == NULL, which might indicate that the
  45.  * gMIDIClient object was never created. This provides another check against programmer
  46.  * stupidity (present company excepted…).
  47.  *--------------------------------------------------------------------------------------
  48.  */
  49. OSErr CMIDIPort::IMIDIPort(MIDIPortParamsPtr portParams, short bufSize)
  50. {
  51.     short            theRefNum;
  52.     unsigned char *    theLen;
  53.  
  54.     if ((gMIDIClient == 0) ||
  55.         ((itsVersion = gMIDIClient->GetShortVerNum()) == 0))
  56.     {
  57.         itsVersion = itsPortID = itsRefNum = 0;
  58.         return (ErrNoMIDI);
  59.     }
  60.  
  61. // Otherwise, call MIDIAddPort
  62.  
  63.     theLen = &(portParams->name[0]);        // Truncate name if necessary
  64.     if (*theLen > midiMaxNameLen) *theLen = midiMaxNameLen;
  65.  
  66.     itsPortID = portParams->portID;            // Save our port ID in the object
  67.     itsResult = MIDIAddPort(gSignature,        // Call MIDI Manager
  68.                             bufSize, &theRefNum, portParams);
  69.     itsRefNum = theRefNum;                    // Save the port reference number
  70.     return itsResult;
  71. }
  72.  
  73. /*
  74.  *--- CMIDIPort::Dispose ----------------------------------------------------
  75.  * Dispose of this MIDI port.  Note that we do NOT call MIDIRemovePort()
  76.  * here.  This has been seen to cause problems with the MIDI Manager.
  77.  *
  78.  * Normally, this isn't a problem, since you typically would only call
  79.  * this when you are quitting.  If you specifically need to call
  80.  * MIDIRemovePort(), then you can override this method.
  81.  *---------------------------------------------------------------------------
  82.  */
  83. void CMIDIPort::Dispose(void)
  84. {
  85.     inherited::Dispose();
  86. }
  87.  
  88.  
  89. /*
  90.  *--- CMIDIDataPort::LoadPatches ----------------------------------
  91.  * This is a virtual function which is overloaded in subclasses
  92.  * CMIDIDataPort and CMIDITimePort.
  93.  *-----------------------------------------------------------------
  94.  */
  95. OSErr CMIDIPort::LoadPatches(ResType theResType, short theResID)
  96. {
  97.     this->SubclassResponsibility();
  98.     return noErr;
  99. }
  100.  
  101. /*
  102.  *--- CMIDIPort::SavePatches -------------------------------------------------
  103.  * Saves the current port connections in a specified resource.
  104.  *----------------------------------------------------------------------------
  105.  */
  106. OSErr CMIDIPort::SavePatches(ResType theResType, short theResID)
  107. {
  108.     Str255                theName;
  109.     Handle                h;
  110.     MIDIPortInfoHdl        portInfo;
  111.  
  112.     if ( ! itsVersion )
  113.         return (ErrNoMIDI);
  114.  
  115.     if (itsResult != noErr)            // May have been a virtual connection,
  116.         return(itsResult);            // so don't do anything.
  117.  
  118.     GetPortName(theName);
  119.     h = GetResource(theResType, theResID);    // Delete resource if it exists
  120.     if (h)
  121.     {
  122.         RmveResource(h);
  123.         DisposHandle(h);
  124.         UpdateResFile(CurResFile());
  125.     }
  126.     portInfo = (MIDIPortInfoHdl) MIDIGetPortInfo(gSignature, itsPortID);
  127.     if (portInfo)
  128.     {
  129.         if ((**portInfo).numConnects > 0)    // No connection, no record
  130.         {
  131.             AddResource(portInfo, theResType, theResID, theName);
  132.             WriteResource(portInfo);
  133.             UpdateResFile(CurResFile());
  134.         }
  135.         ReleaseResource(portInfo);
  136.     }
  137.     return (noErr);
  138. }
  139.  
  140. /*
  141.  *--- Misc, trivial methods -------------------------------------------
  142.  * These aren't individually commented because frankly, I didn’t feel
  143.  * like it, nor do they really need any comments besides this one.
  144.  *---------------------------------------------------------------------
  145.  */
  146. short CMIDIPort::GetRefNum(void)
  147. {
  148.     return (itsRefNum);
  149. }
  150.  
  151. long CMIDIPort::GetRefCon(void)
  152. {
  153.     return (itsVersion ? MIDIGetRefCon(itsRefNum) : 0);
  154. }
  155.  
  156. void CMIDIPort::SetRefCon(long theRefCon)
  157. {
  158.     if (itsVersion)
  159.         MIDISetRefCon(itsRefNum, theRefCon);
  160. }
  161.  
  162. void CMIDIPort::GetPortName(StringPtr theName)
  163. {
  164.     if (itsVersion)
  165.         MIDIGetPortName(gSignature, itsPortID, theName);
  166. }
  167.  
  168. void CMIDIPort::SetPortName(StringPtr theName)
  169. {
  170.     if (itsVersion)
  171.     {
  172.         if (theName[0] > midiMaxNameLen)
  173.             theName[0] = midiMaxNameLen;
  174.         MIDISetPortName(gSignature, itsPortID, theName);
  175.     }
  176. }
  177.  
  178. MIDIPortInfoHdl    CMIDIPort::GetPortInfo(void)
  179. {
  180.     return(itsVersion ? MIDIGetPortInfo(gSignature, itsPortID) : 0);
  181. }
  182.  
  183. void CMIDIPort::SetConnectionProc(ProcPtr theConnectProc, long theRefCon)
  184. {
  185.     if (itsVersion >= 0x200)        // Version 2.0 or later
  186.         MIDISetConnectionProc(itsRefNum, theConnectProc, theRefCon);
  187. }
  188.  
  189.  
  190. void CMIDIPort::GetConnectionProc(ProcPtr * theConnectProc, long * theRefCon)
  191. {
  192.     if (itsVersion >= 0x200)        // Version 2.0 or later
  193.         MIDIGetConnectionProc(itsRefNum, theConnectProc, theRefCon);
  194. }
  195.  
  196. // end of CMIDIPort.c